home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.01 Jan 90 / XDemo Source / XDemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-04  |  10.3 KB  |  519 lines  |  [TEXT/MPS ]

  1. /*--------------- XDemo.c --------------*/
  2. /*
  3. A teeny program that demonstrates the use of external functions.
  4. By Mark Lankton, 1989, for MacTutor.
  5. */
  6.  
  7. #if !defined(USEDUMP)
  8.  
  9. #include <types.h>
  10. #include <resources.h>
  11. #include <quickdraw.h>
  12. #include <windows.h>
  13. #include <OSUtils.h>
  14. #include <OSEvents.h>
  15. #include <memory.h>
  16. #include <fonts.h>                 
  17. #include <events.h>                
  18. #include <controls.h>                
  19. #include <menus.h>                 
  20. #include <dialogs.h>                
  21. #include <desk.h>                    
  22. #include <toolutils.h>             
  23. #include <segload.h>                
  24. #include <DiskInit.h>
  25.  
  26.     #if defined(MAKEDUMP)
  27.         #pragma dump "HeaderDumpFile"
  28.     #endif
  29.     
  30. #else
  31.     #pragma load "HeaderDumpFile"
  32.     
  33. #endif
  34.  
  35. #include "XDemo.h"
  36.  
  37. extern _DataInit();
  38.  
  39. /*------------- prototypes ----------------*/
  40. void    InitVars(void);
  41. void    SetUpMenus(void);
  42. void    OpenAWindow(void);
  43. void    DoEvents(void);
  44. void    DoActivate(WindowPtr thisWindow,Boolean becomingActive);
  45. void    DoUpdate(WindowPtr thisWindow);
  46. void    Redraw(WindowPtr thisWindow);
  47. void     DoGoAway(WindowPtr thisWindow);
  48. void    DoGrow(WindowPtr thisWindow);
  49. void    DoWindowZoom(WindowPtr thisWindow,short partCode);
  50. void    DoContent(WindowPtr thisWindow);
  51. void    DoKeys(void);
  52. void    DoCommand(long menuResult);
  53. void    DoAbout(void);
  54. int        main(void);
  55.  
  56. /*Here's the place we'll plug in the external functions. */
  57. pascal Boolean    (*theXTRAProc)(XTRABlock *theBlockPtr);
  58.  
  59. /*-------------- functions -----------------*/
  60. void
  61. InitVars()
  62. {
  63.     screenRect = qd.screenBits.bounds;
  64.     ClipRect(&screenRect);
  65.     
  66.     SetRect(&dragRect,4,24,screenRect.right-4,
  67.             screenRect.bottom-4);
  68.             
  69.     SetRect(&defaultWRect,screenRect.left,screenRect.top,
  70.                             screenRect.left + 500, screenRect.top + 200);
  71.     OffsetRect(&defaultWRect,10,40);
  72.     
  73.     eventRgn = NewRgn();    /*set up an empty region to pass to WaitNextEvent */
  74.     RectRgn(eventRgn,&defaultWRect);
  75.         
  76.     screenDataLength = 512;      /*A completely arbitrary value... */
  77.     /*Get a pointer to a little data area and fill it with zeroes. */
  78.     screenData = (short *)NewPtrClear(screenDataLength * 2);
  79.     /*Now a very rudimentary error check...*/
  80.     if(!screenData)
  81.         {
  82.         SysBeep(5);
  83.         ExitToShell();
  84.         }
  85. }
  86.  
  87. void
  88. SetUpMenus()
  89. {
  90.     int            i,howMany;
  91.     short        thisID;
  92.     ResType        thisType;
  93.     Str255        thisName;
  94.     
  95.     myMenus[appleMenu] = GetMenu(appleID);
  96.     AddResMenu(myMenus[appleMenu], (ResType) 'DRVR');
  97.     
  98.     myMenus[fileMenu] = GetMenu(fileID);
  99.     myMenus[editMenu] = GetMenu(editID);
  100.     myMenus[externalsMenu] = GetMenu(externalsID);
  101.  
  102.     for (i = 1;i <= menuCount;i++)            
  103.         InsertMenu(myMenus[i],0);
  104.         
  105.     /*Check for XTRA resources... if any, load them in and plug the names into
  106.     the Externals menu. We use the '1' resource calls because the current resource
  107.     file is XDemo itself.
  108.     */
  109.     howMany = Count1Resources('XTRA');
  110.     if(howMany > 32)
  111.         howMany = 32;
  112.     for(i = 1;i <= howMany;i++)
  113.         {
  114.         XTRAArray[i] = Get1IndResource('XTRA',i);
  115.         if(!XTRAArray[i])
  116.             break;
  117.         MoveHHi(XTRAArray[i]);
  118.         HLock(XTRAArray[i]);
  119.         GetResInfo(XTRAArray[i],&thisID,&thisType,thisName);
  120.         AppendMenu(myMenus[externalsMenu],thisName);
  121.         }
  122.         
  123.     DrawMenuBar();
  124.  
  125. }
  126.  
  127. void
  128. OpenAWindow()
  129. {
  130.     short    width,height;
  131.     
  132.     theWindow = GetNewWindow(128,(Ptr)nil,(WindowPtr)-1);
  133.     SetPort(theWindow);
  134.  
  135.     width = defaultWRect.right - defaultWRect.left;
  136.     height = defaultWRect.bottom - defaultWRect.top;
  137.     SizeWindow(theWindow,width,height,TRUE);
  138.         
  139.     MoveWindow(theWindow,defaultWRect.left,defaultWRect.top,TRUE);
  140.     ShowWindow(theWindow);
  141.     
  142.     /*We'll only have one window at a time, so... */
  143.     DisableItem(myMenus[fileMenu],newItem);
  144. }
  145.  
  146.  
  147. void
  148. DoEvents()
  149. {
  150.     short        whatCode;
  151.     Point        dialogPoint;
  152.     Boolean        ignoreResult;
  153.     short        resultCode;
  154.     WindowPtr    whichWindow;
  155.  
  156.     if(!inBackground)
  157.         sleepTime = 0x0A;
  158.     else
  159.         sleepTime = 0xFF;
  160.             
  161.     /*No cursor tricks in this demo, just make sure it's the arrow. */
  162.     InitCursor();
  163.         
  164.     ignoreResult = WaitNextEvent(everyEvent,&myEvent,sleepTime,eventRgn);
  165.     switch (myEvent.what)
  166.         {
  167.         case nullEvent:            break;
  168.         
  169.         case mouseDown: whatCode = FindWindow(myEvent.where,&whichWindow);
  170.             switch (whatCode){
  171.                 case inMenuBar: DoCommand(MenuSelect(myEvent.where));
  172.                                 break;
  173.                 case inSysWindow: SystemClick(&myEvent,whichWindow);
  174.                                   break;
  175.                 case inDrag:     DragWindow(whichWindow,myEvent.where,
  176.                                         &dragRect);
  177.                                  break;
  178.                              
  179.                 case inGoAway:    DoGoAway(whichWindow);
  180.                                 break;
  181.                                 
  182.                 case inGrow:    DoGrow(whichWindow);
  183.                                 break;
  184.                 
  185.                 case inContent:    DoContent(whichWindow);
  186.                                 break;
  187.                                 
  188.                 case inZoomIn:    if (TrackBox(whichWindow,myEvent.where,inZoomIn))
  189.                                     {
  190.                                     DoWindowZoom(whichWindow,inZoomIn);
  191.                                     }
  192.                                 break;
  193.                                 
  194.                 case inZoomOut:    if (TrackBox(whichWindow,myEvent.where,inZoomOut))
  195.                                     {
  196.                                     DoWindowZoom(whichWindow,inZoomOut);
  197.                                     }
  198.                                 break;
  199.                 
  200.             }
  201.             break;
  202.             
  203.         case keyDown:
  204.         case autoKey:    DoKeys();
  205.                         break;
  206.         
  207.         case activateEvt:
  208.                         DoActivate((WindowPtr)myEvent.message,
  209.                                             myEvent.modifiers & activeFlag);
  210.                         break;    
  211.         
  212.         case updateEvt: DoUpdate((WindowPtr)myEvent.message);
  213.                         break;
  214.                         
  215.         case diskEvt:    if(HiWord(myEvent.message))
  216.                             {
  217.                             SetPt(&dialogPoint,85,90);
  218.                             resultCode = DIBadMount(dialogPoint,myEvent.message);
  219.                             }
  220.                         break;
  221.                             
  222.                 
  223.         case osEvent:
  224.             switch (myEvent.message >> 24) 
  225.                 {     
  226.                 case mouseMovedMessage:        break;
  227.                 
  228.                 case suspendResumeMessage:    
  229.                         inBackground = ((myEvent.message & resumeMask) == 0);
  230.                         DoActivate(FrontWindow(), !inBackground);
  231.                         break;
  232.                 }
  233.                 break;
  234.                 
  235.         }
  236.     
  237.     
  238. }
  239.  
  240.  
  241. void
  242. DoActivate(thisWindow,becomingActive)
  243.     WindowPtr    thisWindow;
  244.     Boolean        becomingActive;
  245. {            
  246.  
  247.     SetPort(thisWindow);
  248.     
  249.     if (((WindowPeek)thisWindow)->windowKind >= 8)
  250.         {
  251.         DrawGrowIcon(thisWindow);
  252.         
  253.         if (becomingActive)
  254.             {
  255.             theWindow = thisWindow;
  256.             RectRgn(eventRgn,&(thisWindow->portRect));
  257.             DisableItem(myMenus[editMenu],0);
  258.             }
  259.         else
  260.             EnableItem(myMenus[editMenu],0);
  261.         }
  262. }
  263.  
  264. void
  265. DoUpdate(thisWindow)
  266.     WindowPtr    thisWindow;
  267. {
  268.     GrafPtr        savedPort;
  269.     
  270.     GetPort(&savedPort);
  271.     SetPort(thisWindow);
  272.     
  273.     if (((WindowPeek)thisWindow)->windowKind >= 8)
  274.         {
  275.         ClipRect(&screenRect);
  276.         BeginUpdate(thisWindow);
  277.         EraseRect(&thisWindow->portRect);
  278.         DrawGrowIcon(thisWindow);
  279.         Redraw(thisWindow);
  280.         EndUpdate(thisWindow);
  281.         }
  282.     SetPort(savedPort);
  283. }
  284.  
  285. void
  286. Redraw(thisWindow)
  287.     WindowPtr    thisWindow;
  288. {
  289.     GrafPtr        savedPort;
  290.     Rect        localRect;
  291.     short        center;
  292.     int            i;
  293.     
  294.     GetPort(&savedPort);
  295.     SetPort(thisWindow);
  296.     
  297.     localRect = thisWindow->portRect;
  298.     localRect.right -= scrollBarAdjust;
  299.     localRect.bottom -= scrollBarAdjust;
  300.     ClipRect(&localRect);
  301.     
  302.     center = localRect.bottom / 2;
  303.     MoveTo(0,center);
  304.     PenPat(qd.gray);
  305.     Line(localRect.right,0);
  306.     PenPat(qd.black);
  307.     MoveTo(0,center);
  308.     
  309.     for(i = 0; i < screenDataLength;i++)
  310.         LineTo(i,center - *(screenData + i));
  311.     
  312.     SetPort(savedPort);
  313. }
  314.  
  315. void
  316. DoGoAway(thisWindow)
  317.     WindowPtr    thisWindow;
  318. {
  319.     /*
  320.     Our windows have the default windowKind (8)... don't
  321.     try to close someone else's window!
  322.     */
  323.     if(((WindowPeek)thisWindow)->windowKind < 8)
  324.         return;
  325.         
  326.     DisposeWindow(thisWindow);
  327.  
  328.     if(GetNextEvent(everyEvent,&myEvent))
  329.         DoActivate((WindowPtr)myEvent.message,myEvent.modifiers & activeFlag);
  330.     if(GetNextEvent(everyEvent,&myEvent))
  331.         DoActivate((WindowPtr)myEvent.message,myEvent.modifiers & activeFlag);
  332.     
  333.     /*Turn the "New" item back on so we 
  334.     can make another window if we want. */
  335.     EnableItem(myMenus[fileMenu],newItem);
  336. }
  337.  
  338. void
  339. DoGrow(thisWindow)
  340.     WindowPtr    thisWindow;
  341. {
  342.     Rect    sizeRect;
  343.     long    newSize;
  344.     int        newWidth, newHeight;
  345.     
  346.     if (thisWindow != FrontWindow())
  347.         SelectWindow(thisWindow);
  348.     else 
  349.         {        
  350.         SetRect(&sizeRect, minWidth,minHeight,screenRect.right,
  351.                     screenRect.bottom - mBarHeight);
  352.         newSize = GrowWindow(thisWindow,myEvent.where,&sizeRect);
  353.         if (newSize)
  354.             {
  355.             newWidth = LoWord(newSize);
  356.             newHeight = HiWord(newSize);
  357.             SizeWindow(thisWindow,newWidth,newHeight,TRUE);
  358.             
  359.             InvalRect(&(thisWindow->portRect));    
  360.             }
  361.         }
  362. }
  363.  
  364. void
  365. DoWindowZoom(thisWindow,partCode)
  366.     WindowPtr    thisWindow;
  367.     short    partCode;
  368. {
  369.     
  370.     EraseRect(&(thisWindow->portRect));
  371.     ZoomWindow(thisWindow,partCode,TRUE);
  372.     
  373.     InvalRect(&thisWindow->portRect);
  374. }
  375.  
  376. void
  377. DoContent(thisWindow)
  378.     WindowPtr    thisWindow;
  379. {
  380.     /*Not much action here... */
  381.     if (thisWindow != FrontWindow())
  382.         SelectWindow(thisWindow);
  383. }    
  384.  
  385. void
  386. DoKeys()
  387. {
  388.     short    temp;
  389.     long      menuChoice;
  390.     
  391.     /*Only worry about command-key stuff. */
  392.     temp = (short)(myEvent.message & charCodeMask);
  393.     if (myEvent.modifiers & cmdKey)
  394.         {
  395.         if (myEvent.what != autoKey)
  396.             {
  397.             menuChoice = MenuKey(temp);
  398.             DoCommand(menuChoice);
  399.             }
  400.         }
  401. }
  402.  
  403. void
  404. DoCommand(menuResult)
  405.     long    menuResult;
  406. {
  407.     Str255         name;
  408.     short         theMenu,theItem;
  409.     XTRABlock    theBlock;
  410.     Boolean        theResult;
  411.     
  412.     theMenu = HiWord(menuResult);    /*Gives you the resource ID. */
  413.     theItem = LoWord(menuResult);
  414.  
  415.     switch (theMenu){
  416.         case appleID: 
  417.             if (theItem == aboutMeItem)
  418.                 {
  419.                  DoAbout();
  420.                  break;
  421.                 }
  422.                 else    EnableItem(myMenus[3],undoItem);
  423.                         GetItem(myMenus[1], theItem, name);
  424.                         (void)OpenDeskAcc(name);
  425.                         break;
  426.         
  427.         case fileID: 
  428.             switch (theItem)
  429.                 {
  430.                 case newItem:            OpenAWindow();
  431.                                         break;
  432.                                         
  433.                 case closeItem:            DoGoAway(FrontWindow());
  434.                                         break;
  435.                 
  436.                 case quitItem:             allDone = TRUE;
  437.                                         break;
  438.                 }
  439.             break;
  440.         
  441.         /*We don't use the Edit menu at all. */
  442.         case editID:        break;
  443.                 
  444.         case externalsID:    theBlock.dataLength = screenDataLength;
  445.                             theBlock.theData = screenData;
  446.                             (ProcPtr)theXTRAProc = (ProcPtr)*(XTRAArray[theItem]);
  447.                             theResult = (*theXTRAProc)(&theBlock);
  448.                             if(!theResult)
  449.                                 /*Do something here */
  450.                                 ;
  451.                             InvalRect(&(theWindow->portRect));
  452.                             break;
  453.         
  454.     }
  455.     HiliteMenu(0);
  456. }  
  457.  
  458. void
  459. DoAbout()
  460. {
  461.     DialogPtr        thisDialog;
  462.     GrafPtr            savedPort;
  463.     Boolean            wait = TRUE;
  464.     Point            thePoint;
  465.     
  466.     GetPort(&savedPort);
  467.     thisDialog = GetNewDialog(aboutID,nil,(WindowPtr)-1);
  468.     SetPort(thisDialog);
  469.     
  470.     DrawDialog(thisDialog);
  471.     
  472.     while (wait)
  473.         {
  474.         if (GetNextEvent(mDownMask,&myEvent))
  475.             {
  476.             thePoint = myEvent.where;
  477.             GlobalToLocal(&thePoint);
  478.             if (PtInRect(thePoint,&thisDialog->portRect))
  479.                 wait = FALSE;
  480.             else
  481.                 SysBeep(5);
  482.             }
  483.         }
  484.     
  485.     SetPort(savedPort);
  486.     DisposDialog(thisDialog);
  487.     
  488. }
  489.  
  490. int
  491. main()
  492. {
  493.     UnloadSeg(_DataInit);
  494.     InitGraf(&qd.thePort);
  495.     InitFonts();
  496.     FlushEvents(everyEvent,0);
  497.     InitWindows();
  498.     InitMenus();
  499.     TEInit();
  500.     InitDialogs(nil);
  501.     InitCursor();
  502.     
  503.     MaxApplZone();
  504.     MoreMasters();
  505.     MoreMasters();
  506.         
  507.     InitVars();
  508.     SetUpMenus();
  509.     OpenAWindow();    
  510.  
  511.     do {
  512.         DoEvents();
  513.         } while (allDone == FALSE);
  514.         
  515.     
  516.  
  517.     ExitToShell();
  518.  
  519. }